// pages/message/message.js
var app = getApp();
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: '',
    list: [],
    page: 1,
    sizes: 0,
    total: 0,
    currentIndex: ''
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
  },
  onShow: function () {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    var url = host + '/yq/user/message';
    var params = {};
    var success = function (res) {
      console.log(res);
      self.setData({
        list: res.info,
        page: res.page,
        size: res.size,
        total: res.total
      })
    };
    var fail = function (res) { };
    var complete = function () { };
    app.request(url, params, success, fail, complete);
  },
  onTapMsg(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var isNew = e.currentTarget.dataset.isnew;
    if (Number(index) === self.data.currentIndex) {
      self.setData({
        currentIndex: ''
      })
    } else {
      self.setData({
        currentIndex: Number(index)
      })
      if (isNew == "1") {
        wx.request({
          url: host + '/yq/user/readMsg',
          data: {
            id: id
          }
        })
        var list = self.data.list;
        list[index].isNew = 0;
        self.setData({
          list: list
        })
      }
    }
  }
})